Passed
Push — develop ( 8f0915...c2f2a9 )
by
unknown
02:45
created

helper.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import i18n from './i18n.js'
2
import color from 'color'
3
import moment from 'moment'
4
5
export const libHandleFetchResult = async fetchResult => {
6
  switch (fetchResult.status) {
7
    case 200:
8
    case 304:
9
      const resultJson = await fetchResult.clone().json()
10
      return new Promise((resolve, reject) => resolve({
11
        apiResponse: fetchResult,
12
        body: resultJson
13
      }))
14
    case 204:
15
      return fetchResult
16
    case 400:
17
    case 404:
18
    case 409:
19
    case 500:
20
    case 501:
21
    case 502:
22
    case 503:
23
    case 504:
24
      return new Promise((resolve, reject) => reject(fetchResult)) // @TODO : handle errors from api result
25
  }
26
}
27
28
export const libAddAllResourceI18n = (i18n, translation) => {
29
  Object.keys(translation).forEach(lang =>
30
    Object.keys(translation[lang]).forEach(namespace =>
31
      i18n.addResources(lang, namespace, translation[lang][namespace])
32
    )
33
  )
34
}
35
36
export const libGenerateAvatarFromPublicName = publicName => {
37
  // code from https://stackoverflow.com/questions/3426404/create-a-hexadecimal-colour-based-on-a-string-with-javascript
38
  const stringToHashCode = str => str.split('').reduce((acc, char) => char.charCodeAt(0) + ((acc << 5) - acc), 0)
39
40
  const intToRGB = i => {
41
    const c = (i & 0x00FFFFFF).toString(16).toUpperCase()
42
    return '00000'.substring(0, 6 - c.length) + c
43
  }
44
45
  const hexcolor = '#' + intToRGB(stringToHashCode(publicName))
46
47
  let canvas = document.createElement('canvas')
48
49
  // http://code.google.com/p/explorercanvas/wiki/Instructions#Dynamically_created_elements
50
  if (!canvas.getContext) G_vmlCanvasManager.initElement(canvas)
51
52
  let ctx = canvas.getContext('2d')
53
  canvas.width = 44
54
  canvas.height = 44
55
56
  const { r, g, b } = color(hexcolor).desaturate(0.75).rgb()
57
58
  ctx.beginPath()
59
  ctx.arc(22, 22, 20, 0, 2 * Math.PI, false)
60
  ctx.fillStyle = 'rgba(' + [r, g, b, 1].join() + ')'
61
  ctx.fill()
62
  ctx.stroke()
63
64
  return canvas.toDataURL('image/png', '')
65
}
66
67
export const libDisplayDate = (dateToDisplay, lang) => {
68
  i18n.changeLanguage(lang)
69
70
  const todayMoment = moment(new Date())
71
  const dateToDisplayMoment = moment(new Date(dateToDisplay))
72
73
  const diffDay = todayMoment.diff(dateToDisplayMoment, 'days')
74
  if (diffDay > 0) return i18n.t('{{nb}} days ago', {nb: diffDay})
75
76
  const diffHour = todayMoment.diff(dateToDisplayMoment, 'hours')
77
  if (diffHour > 0) return i18n.t('{{nb}} hours ago', {nb: diffHour})
78
79
  const diffMinute = todayMoment.diff(dateToDisplayMoment, 'minutes')
80
  if (diffMinute > 0) return i18n.t('{{nb}} minutes ago', {nb: diffMinute})
81
82
  const diffSeconde = todayMoment.diff(dateToDisplayMoment, 'seconds')
83
  return i18n.t('{{nb}} seconds ago', {nb: diffSeconde})
84
}
85